package com.nkl.page.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.nkl.common.dao.BaseDao;
import com.nkl.common.util.StringUtil;
import com.nkl.page.domain.Infos;

/**
 * 后端SQL语句
 */
public class InfosDao {

	public int addInfos(Infos infos, Connection conn){
		String sql = "INSERT INTO infos(info_id,info_title,info_content,info_picture,info_time,info_admin,info_click,info_type) values(null,?,?,?,?,?,?,?)";
		Object[] params = new Object[] {
			infos.getInfo_title(),
			infos.getInfo_content(),
			infos.getInfo_picture(),
			infos.getInfo_time(),
			infos.getInfo_admin(),
			infos.getInfo_click(),
			infos.getInfo_type()

		};
		return BaseDao.executeUpdate(sql, params, conn );
	}

	public int delInfos(String info_id, Connection conn){
		String sql = "DELETE FROM infos WHERE info_id=?";

		Object[] params = new Object[] { new Integer(info_id)};
		return BaseDao.executeUpdate(sql, params, conn);
	}

	public int delInfoss(String[] info_ids, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i <info_ids.length; i++) {
			sBuilder.append("?");
			if (i !=info_ids.length-1) {
				sBuilder.append(",");
			}
		}
		String sql = "DELETE FROM infos WHERE info_id IN(" +sBuilder.toString()+")";

		Object[] params = info_ids;

		return BaseDao.executeUpdate(sql, params, conn);	}

	public int updateInfos(Infos infos, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("UPDATE infos SET info_id = " + infos.getInfo_id() +" ");
		if (!StringUtil.isEmptyString(infos.getInfo_title())) {
			sBuilder.append(" , info_title ='" + infos.getInfo_title() + "' ");
		}
		if (!StringUtil.isEmptyString(infos.getInfo_content())) {
			sBuilder.append(" , info_content ='" + infos.getInfo_content() + "' ");
		}
		if (!StringUtil.isEmptyString(infos.getInfo_admin())) {
			sBuilder.append(" , info_admin ='" + infos.getInfo_admin() + "' ");
		}
		if (!StringUtil.isEmptyString(infos.getInfo_picture())) {
			sBuilder.append(" , info_picture ='" + infos.getInfo_picture() + "' ");
		}
		if (infos.getInfo_click()!=0) {
			sBuilder.append(" , info_click =" + infos.getInfo_click() + " ");
		}

		sBuilder.append(" where info_id = " + infos.getInfo_id() );

		Object[] params = null;
		return BaseDao.executeUpdate(sBuilder.toString(), params, conn);	}

	public Infos getInfos(Infos infos, Connection conn){
		Infos _infos=null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM infos WHERE info_id = "+ infos.getInfo_id() );

		List<Object> list = BaseDao.executeQuery(Infos.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			 _infos = (Infos)list.get(0);
		}
		return _infos;
	}

	public List<Infos>  listInfoss(Infos infos, Connection conn){
		List<Infos> infoss = null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM (");
		sBuilder.append("SELECT * FROM infos  WHERE 1=1");
		
		if (infos.getInfo_id() !=0) {
			sBuilder.append(" and info_id = " + infos.getInfo_id());
		}
		if (!StringUtil.isEmptyString(infos.getInfo_title())) {
			sBuilder.append(" and info_title like '%" + infos.getInfo_title() + "%'");
		}
		if (infos.getInfo_type() !=0) {
			sBuilder.append(" and info_type = " + infos.getInfo_type());
		}
		
		sBuilder.append(" order by info_time desc,info_id asc) t");
		
		if (infos.getStart() != -1) {
			sBuilder.append(" limit " + infos.getStart() + "," + infos.getLimit());
		}
		
		List<Object> list = BaseDao.executeQuery(Infos.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			infoss = new ArrayList<Infos>();
			for (Object object : list) {
				infoss.add((Infos)object);
			}
		}
		return infoss;
	}
	
	public int listInfossCount(Infos infos, Connection conn){
		int sum = 0;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT count(*) FROM infos WHERE 1=1");
		
		if (infos.getInfo_id() !=0) {
			sBuilder.append(" and info_id = " + infos.getInfo_id());
		}
		if (!StringUtil.isEmptyString(infos.getInfo_title())) {
			sBuilder.append(" and info_title like '%" + infos.getInfo_title() + "%'");
		}
		if (infos.getInfo_type() !=0) {
			sBuilder.append(" and info_type = " + infos.getInfo_type());
		}
		
		long count = (Long)BaseDao.executeQueryObject(sBuilder.toString(), null, conn);
		sum = (int)count;
		
		return sum;
	}

}
